package com.example.zwgt.yrfk.ui.main.fragment;


import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.example.zwgt.yrfk.R;
import com.example.zwgt.yrfk.base.mvpbase.BasePresenter;
import com.example.zwgt.yrfk.base.mvpbase.baseImpl.BaseFragment;
import com.example.zwgt.yrfk.bean.TestBean;
import com.example.zwgt.yrfk.ui.access.activity.AccessDetailActivity;
import com.example.zwgt.yrfk.ui.access.activity.AddUserActivity;
import com.example.zwgt.yrfk.ui.access.activity.SearchActivity;
import com.example.zwgt.yrfk.ui.access.adapter.AccessAdapter;
import com.example.zwgt.yrfk.utils.Util;
import com.example.zwgt.yrfk.view.SelectPopWindow;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.header.progresslayout.ProgressLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class AccessFragment extends BaseFragment {
    @BindView(R.id.tv_screen)
    TextView tvScreen;
    @BindView(R.id.iv_add)
    ImageView ivAdd;
    @BindView(R.id.video)
    TextView video;
    @BindView(R.id.recycle)
    RecyclerView recycle;
    @BindView(R.id.rl_top)
    RelativeLayout rl_top;
    AccessAdapter adapter;
    @BindView(R.id.refreshLayout)
    TwinklingRefreshLayout refreshLayout;
    private SelectPopWindow popWindow;
    private List<TestBean> beans = new ArrayList<>();


    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_access;
    }

    @Override
    protected void initView() {
        popWindow = new SelectPopWindow(getContext());
        for (int i = 0; i <= 30; i++) {
            beans.add(new TestBean("20150307", "20万", "小明", "审批完成"));
        }
        View headerView = View.inflate(getActivity(),
                R.layout.header_guest_fragment, null);
        headerView.setOnClickListener(view -> SearchActivity.startAction(getContext()));
        adapter = new AccessAdapter(R.layout.guest_item, beans);
        adapter.addHeaderView(headerView);
        adapter.openLoadAnimation();
        recycle.setLayoutManager(new LinearLayoutManager(getActivity()));
        recycle.setAdapter(adapter);
        adapter.setOnItemClickListener((ad, view, position) -> AccessDetailActivity.startAction(getContext(), adapter.getItem(position).getHead_img(), adapter.getItem(position).getNickname(),
                adapter.getItem(position).getPhone(), adapter.getItem(position).getId()));
        //popWindow点击事件
        popWindow.setOnItemClick(position -> {
            ToastUtils.showShort(position + "");
        });

        ProgressLayout headView = new ProgressLayout(getContext());
        headView.setColorSchemeColors(ContextCompat.getColor(getContext(), R.color.colorAccent), ContextCompat.getColor(getContext(), R.color.yellow),
                ContextCompat.getColor(getContext(), R.color.Green));

        refreshLayout.setHeaderView(headView);
        refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        refreshLayout.onFinishRefresh();
                    }
                },1500);
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                       refreshLayout.finishLoadmore();
                    }
                },1500);
            }
        });
    }

    @Override
    public BasePresenter initPresenter() {
        return null;
    }

    @OnClick({R.id.tv_screen, R.id.iv_add, R.id.video})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_screen:
                showPopWindow();
                break;
            case R.id.iv_add:
                AddUserActivity.startAction(getContext());
                break;
        }
    }

    private void showPopWindow() {
        //pop出现屏幕变为透明
        WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
        lp.alpha = 0.8f;
        getActivity().getWindow().setAttributes(lp);
        popWindow.showAtLocation(rootView, Gravity.TOP | Gravity.RIGHT, 0, Util.getStatusBarHeight() + rl_top.getBottom());
        popWindow.setOnDismissListener(() -> {
            WindowManager.LayoutParams lp1 = getActivity().getWindow().getAttributes();
            lp1.alpha = 1.0f;
            getActivity().getWindow().setAttributes(lp1);
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }
}
